<?php

namespace App\Http\Requests\Admin\GameTool;

use App\Models\Enum\MailType;
use Illuminate\Foundation\Http\FormRequest;

//添加批量邮件 请求验证
class MailBatchCreateRequest extends FormRequest
{
    public function __construct()
    {

    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'channel_id' => 'required',
            'title' => 'required',
            'content' => 'required',
            'reason' => 'required',
            'attachment' => 'required|mimes:csv,txt'
        ];

        return $rules;
    }

    public function messages()
    {
        return [
            'channel_id.required' => '渠道不能为空',
            'title.required' => '邮件标题不能为空',
            'content.required' => '邮件内容不能为空',
            'reason.required' => '发放理由不能为空',

            'attachment.required' => '附件不能为空',
            'attachment.mimes' => '必须上传csv格式的文件',
        ];
    }

    protected function prepareForValidation()
    {
        $item_1 = $this->_filter($this->item_id_1);
        $item_type_1 = $item_1['item_type'];
        $item_id_1 = $item_1['item_id'];

        $item_2 = $this->_filter($this->item_id_2);
        $item_type_2 = $item_2['item_type'];
        $item_id_2 = $item_2['item_id'];

        $item_3 = $this->_filter($this->item_id_3);
        $item_type_3 = $item_3['item_type'];
        $item_id_3 = $item_3['item_id'];

        $item_4 = $this->_filter($this->item_id_4);
        $item_type_4 = $item_4['item_type'];
        $item_id_4 = $item_4['item_id'];

        $item_5 = $this->_filter($this->item_id_5);
        $item_type_5 = $item_5['item_type'];
        $item_id_5 = $item_5['item_id'];

        $item_num_1 = ($this->item_num_1 === null) ? 0 : $this->item_num_1;
        $item_num_2 = ($this->item_num_2 === null) ? 0 : $this->item_num_2;
        $item_num_3 = ($this->item_num_3 === null) ? 0 : $this->item_num_3;
        $item_num_4 = ($this->item_num_4 === null) ? 0 : $this->item_num_4;
        $item_num_5 = ($this->item_num_5 === null) ? 0 : $this->item_num_5;

        $this->merge([
            'item_type_1' => $item_type_1,
            'item_type_2' => $item_type_2,
            'item_type_3' => $item_type_3,
            'item_type_4' => $item_type_4,
            'item_type_5' => $item_type_5,

            'item_id_1' => $item_id_1,
            'item_id_2' => $item_id_2,
            'item_id_3' => $item_id_3,
            'item_id_4' => $item_id_4,
            'item_id_5' => $item_id_5,

            'item_num_1' => $item_num_1,
            'item_num_2' => $item_num_2,
            'item_num_3' => $item_num_3,
            'item_num_4' => $item_num_4,
            'item_num_5' => $item_num_5,
        ]);
    }

    private function _filter($item_id)
    {
        if (empty($item_id)) {
            return ['item_type' => 0, 'item_id' => ''];
        }

        $item_id = explode('-', $item_id);
        return ['item_type' => $item_id[0], 'item_id' => $item_id[1]];
    }
}
